import { useAuthStore } from '@/store/index.js'

let isConfirming = false

export function resolveResError(code, message, needTip = true) {
  switch (code) {
    case 1010:
      if (isConfirming || !needTip)
        return
      isConfirming = true
      window.$messageBox
        .confirm('登录信息已过期!', '提示', {
          confirmButtonText: '确定',
          showCancelButton: false,
          type: 'info',
          closeOnClickModal: false,
        })
        .then(() => {
          useAuthStore().logout()
        })
        .catch(() => {
          isConfirming = false
        })
      return false
    case 404:
      message = '请求资源或接口不存在'
      break
    case 500:
      message = '服务器发生异常'
      break
    default:
      message = message ?? `【${code}】: 未知异常!`
      break
  }
  needTip && window.$message?.error(message)
  return message
}
